﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.ReturnedDocuments;
using System.Collections.Generic;
using MS.InformationManagement.DAL.DTOs;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.WP.DocumentCenter.ReceptionReturnedDocuments
{
    public partial class ReceptionReturnedDocumentsUserControl : BaseControl, IReturnedDocumentsView
    {

        #region fields

        ReturnedDocumentsPresenter _presenter;

        #endregion

        #region Properties

        public IEnumerable<ReturnedDocumentDTO> FoundedItems { get; set; }
        public string RadicadoId { get { return txtRadicadoId.Text.Trim(); } }

        #endregion

        #region form events

        protected override void OnInit(EventArgs e)
        {
            try
            {
                base.OnInit(e);
                Page.RegisterAjaxLoaderPlugin();
                txtRadicadoId.Required(ExceptionConstants.ERROR_CAMPO_NULO, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                btnSearch.AddLoader();
            }
            catch(Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void gvRadicados_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if(e.Row.RowType != DataControlRowType.DataRow) return;
                ReturnedDocumentDTO dataItem = e.Row.DataItem as ReturnedDocumentDTO;
                HyperLink currentLink = e.Row.FindControl("lnkReturn") as HyperLink;
                currentLink.NavigateUrl = string.Format("/_layouts/ProcessReturnDocument.aspx?currentListId={0}&currentItemId={1}&radicadoId={2}", dataItem.ListId, dataItem.ItemId, dataItem.RadicadoId);
                currentLink.ToolTip = "Registrar devolución";
            }
            catch(Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _presenter = new ReturnedDocumentsPresenter(this);
            }
            catch(Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void search_action(object sender, EventArgs e)
        { 
             try
            {
                _presenter.HandleSearch();
            }
            catch(Exception ex)
            {
                HandleError(ex);
            } 
        }



        #endregion

        #region Implemented Methods

        public void ShowFoundedItems()
        {
            gvRadicados.DataSource = FoundedItems;
            gvRadicados.DataBind();
        }

        #endregion
    }
}
